package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Grade;
import com.xmy.cultivate.entity.Student;
import com.xmy.cultivate.entity.StudentGradeTemporary;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 临时学员，补课学员 Mapper 接口
 * </p>
 *
 * @author min
 * @since 2022-09-22
 */
@Mapper
public interface StudentGradeTemporaryMapper extends BaseMapper<StudentGradeTemporary> {
    /**
     * 获取临时学员列表
     */
    @Select("SELECT t1.id,t4.`name` AS student_name,t1.school_date,t1.time_start,t1.time_end,t1.start_date_time,t1.end_date_time,getSchoolName ( t2.school_id ) AS school_name,t2.`name` AS grade_name,t3.`name` AS course_name,t2.subjects_id,t1.lesson_type,getStaffNameForId(t2.teacher_id) AS teacher_name ,t1.type FROM student_grade_temporary AS t1 LEFT JOIN grade AS t2 ON t1.grade_id = t2.id LEFT JOIN course AS t3 ON t2.course_id =t3.id LEFT JOIN student AS t4 ON t1.student_id = t4.id ${ew.customSqlSegment} ")
    IPage<StudentGradeTemporary> selectStudentGradeTemporaryPage(IPage<StudentGradeTemporary> page, @Param(Constants.WRAPPER) Wrapper wrapper);


    /**
     * 获取可选班级列表
     */
    @Select("SELECT t1.id,t1.school_id,t1.subjects_id,t1.`name`,t1.full_count,COUNT( CASE WHEN t2.student_id IS NOT NULL AND t2.reading_status IN ( 0, 1 ) AND t2.deleted THEN 1 END ) AS student_count FROM grade AS t1 LEFT JOIN student_grade AS t2 ON t1.id = t2.grade_id ${ew.customSqlSegment} ")
    List<Grade> selectGradeList(@Param(Constants.WRAPPER) Wrapper wrapper);


    /**
     * 获取可选学员列表
     */
    @Select("SELECT b.id,a.residue_course_count/3 as residue_course_count,b.`name`,b.head_img,a.handle_date FROM order_detail AS a LEFT JOIN student AS b ON a.student_id = b.id  ${ew.customSqlSegment} ")
    IPage<Student> selectStudentList(IPage<Student> page, @Param(Constants.WRAPPER) Wrapper wrapper);

}
